/*
 * @Descripttion
 * @version
 * @Author         Xiaoqi Lai
 * @Date           2021-07-05 18:01:10
 * @LastEditors    Xiaoqi Lai
 * @LastEditTime   2021-07-05 22:37:07
 */
const {
  resolve4
} = require("dns");
const express = require("express");
const http = require("http");
const path = require("path");

// 启动服务器
const app = express();

app.use(express.static(path.join(__dirname, "public")));

// 首页内容
app.get("/", async (req, res) => {
  res.sendFile(__dirname + "/public/" + "page.html");
});

// 打开LED灯
app.get("/low", (req, res) => {
  http
    .get("http://192.168.2.1/low", (res) => {
      console.log(res);
    })
    .on("error", (e) => {
      console.log(e);
    });
  res.end();
});

// 关闭LED灯
app.get("/high", (req, res) => {
  http
    .get("http://192.168.2.1/high", (res) => {
      console.log(res);
    })
    .on("error", (e) => {
      console.log(e);
    });
  res.end();
});

//获取温度和湿度
app.get("/tandh", (req, ress) => {
  http
    .get("http://192.168.2.1/tandh", (res) => {
      console.log(res);
      console.log('success');
    })
    .on("error", (e) => {
      console.log(e);
      let string;
      if (e.rawPacket != undefined) {
        string = e.rawPacket.toString();
        ress.send({
          msg: string
        });
      } else {
        ress.send({
          msg: 'error'
        })
      }
    });
});

// 控制电机旋转
app.get("/xsb", (req, ress) => {
  // { param: '10' }
  console.log(req.query);
  http
    .get("http://192.168.2.1/xsb?params=" + req.query.param, (res) => {
      console.log(res);
      console.log("success");
    })
    .on("error", (e) => {
      console.log(e);
      let string;
      if (e.rawPacket != undefined) {
        string = e.rawPacket.toString();
        ress.send({
          msg: string
        });
      } else {
        ress.send({
          msg: 'error'
        })
      }
    });
});


// 警报开关
app.get("/xaa", (req, ress) => {
  console.log(req.query);
  http
    .get("http://192.168.2.1/xaa?params=" + req.query.param, (res) => {
      console.log(res);
      console.log("success");
    })
    .on("error", (e) => {
      console.log(e);
      let string;
      if (e.rawPacket != undefined) {
        string = e.rawPacket.toString();
        console.log(string);
        ress.send({
          msg: string
        });
      } else {
        ress.send({
          msg: 'error'
        })
      }
    });
})

// 输入亮度控制灯
app.get("/xlc", (req, ress) => {
  console.log(req.query);
  http
    .get("http://192.168.2.1/xlc?params=" + req.query.param, (res) => {
      console.log(res);
      console.log("success");
    })
    .on("error", (e) => {
      console.log(e);
      let string;
      if (e.rawPacket != undefined) {
        string = e.rawPacket.toString();
        console.log(string);
        ress.send({
          msg: string
        });
      } else {
        ress.send({
          msg: 'error'
        })
      }
    });
})

// 窗监视
app.get("/ws", (req, ress) => {
  http
    .get("http://192.168.2.1/ws", (res) => {
      console.log(res);
      console.log('success');
    })
    .on("error", (e) => {
      console.log(e);
      let string;
      if (e.rawPacket != undefined) {
        string = e.rawPacket.toString();
        ress.send({
          msg: string
        });
      } else {
        ress.send({
          msg: 'error'
        })
      }
    });
});

// 门监视
app.get("/ds", (req, ress) => {
  http
    .get("http://192.168.2.1/ds", (res) => {
      console.log(res);
      console.log('success');
    })
    .on("error", (e) => {
      console.log(e);
      let string;
      if (e.rawPacket != undefined) {
        string = e.rawPacket.toString();
        ress.send({
          msg: string
        });
      } else {
        ress.send({
          msg: 'error'
        })
      }
    });
});

// 初始化
app.get("/init", (req, ress) => {
  http
    .get("http://192.168.2.1/init", (res) => {
      console.log(res);
      console.log('success');
    })
    .on("error", (e) => {
      console.log(e);
      let string;
      if (e.rawPacket != undefined) {
        string = e.rawPacket.toString();
        ress.send({
          msg: string
        });
      } else {
        ress.send({
          msg: 'error'
        })
      }
    });
});

// 设置监听端口
app.listen(3000, '192.168.2.100', () => {
  console.log("app running!");
});